package com.wn.servlet.demo03;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yellow Docter
 * @date 2022 -06-28
 * @desc
 *
 * ConfigServlet  配置servlet信息,在初始化做一些准备工作
 *
 * 获取servlet中配置的参数
 *
 * 01  获取servletconfig
 *        ServletConfig servletConfig = getServletConfig(); 从父类中直接调用方法,获取对象
 * 02  调用方法
 *          servletConfig.getInitParameter("")   获取数据
 *
 *  xml的配置:
 *      <init-param>
 *          <param-name>password</param-name>
 *          <param-value>tiger</param-value>
 *      </init-param>
 *
 *  注解的配置:
 *        initParams 对应的是:  xml中的标签多个<init-param>
 *        @WebInitParam 这个对应的是  <init-param>
 *              属性name      <param-name>
 *              属性value     <param-value>
 *        initParams= {
 *                 @WebInitParam(name = "username",value = "zs"),
 *                 @WebInitParam(name = "password",value = "123"),
 *         }
 */
@WebServlet(
        urlPatterns = "/config",
        initParams= {
                @WebInitParam(name = "username",value = "zs"),
                @WebInitParam(name = "password",value = "123"),
        }
)
public class MyConfigServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        ServletConfig servletConfig = getServletConfig();

        String username = servletConfig.getInitParameter("username");
        System.out.println("username = " + username);

        String password = servletConfig.getInitParameter("password");
        System.out.println("password = " + password);

    }
}
